
package com.meme.editor.property.control;

import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.JSpinner;

import org.apache.log4j.Logger;

import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseWheelListener;

import javax.swing.event.ChangeListener;

import com.meme.editor.property.Property;
import com.meme.editor.property.PropertyValue;
import com.meme.editor.property.PropertyControl;

public class NumberPropertyControl extends PropertyControl implements ChangeListener//, FocusListener//, MouseWheelListener
{
	static Logger logger = Logger.getLogger (NumberPropertyControl.class);
	
	String mLastValue;
	
	JSpinner mSpinner;
	
	public NumberPropertyControl ()
	{
	}
	
	@Override
	public void enableEvents ()
	{
		mSpinner.addChangeListener(this);
		//mSpinner.addFocusListener(this);
		//mTextField.addMouseWheelListener (this);
	}
	
	@Override
	public void disableEvents ()
	{
		mSpinner.removeChangeListener(this);
		//mSpinner.removeFocusListener(this);
	}
	
	@Override
	public void updateFromValue (Object[] controlParams) 
	{
		mLastValue = (String)controlParams[0];
		( (JSpinner) getComponent () ).setValue (Float.parseFloat(mLastValue));
	}
	
	@Override
	public void updateFromValue ()
	{
		mLastValue = getPropetyValue ().toString();
		( (JSpinner) getComponent () ).setValue (Float.parseFloat(mLastValue));
	}
	
	@Override
	public Object[] makeDataForValue ()
	{
		return new Object[] { ( ( (JSpinner) getComponent () ).getValue() ).toString() };
	}
	
//	@Override
//	public void setEditable (boolean bEditable)
//	{		
//		( (JTextField) getComponent () ).setEditable (bEditable);
//	}
	
	@Override
	public JComponent getComponent ()
	{	
		if ( mSpinner == null )
		{
			mSpinner = new JSpinner( );
			mSpinner.addChangeListener(this);
			//mSpinner.addFocusListener(this);
		}
		
		return mSpinner;
	}
	
	@Override
	public void disposeComponent ()
	{
		if ( mSpinner != null )
		{
			mSpinner.removeChangeListener(this);
			//mSpinner.removeFocusListener(this);
		}
		
		mSpinner = null;
	}

	@Override
	public void stateChanged(javax.swing.event.ChangeEvent e)	
	{
		updatePropertyValue ();
	}
	
//	@Override
//	public void focusGained(java.awt.event.FocusEvent arg0)
//	{
//		
//	}
//	  
//	@Override
//	public void focusLost(java.awt.event.FocusEvent arg0)
//	{
//		( (JSpinner) getComponent () ).setValue (Float.parseFloat(mLastValue));	
//
//	}

	@Override
	protected boolean _setParameter (String key, Object param)
	{
		return false;
	}
}
